<?php
namespace plugin\router\src\config;

/**
 * The MIT License
 *
 * Copyright (c) <year> <copyright holders>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 * Classe per il controllo e la gestione delle routes
 *
 * @package lib
 * @subpackage router
 * @author Damiano Ciarla
 */
class RouterConfig
{
	/**
	 * Lista delle route da verificare
	 *
	 * @var array|Route
	 */
	protected $_routes = array();

	/**
	 * Variabile che specifica se le route sono multilingua
	 *
	 * @var boolean
	 */
	public $isMultilanguages = false;
	
	/**
	 * Lista delle lingue accettate
	 *
	 * @var array
	 */
	public $multilanguagesAccepted;
	
	/**
	 * Lingua di default
	 *
	 * @var string
	 */
	public $defaultLang;
	
	/**
	 * Lingua su cui effettuare il controllo della route
	 *
	 * @var string
	 */
	public $currentLang;

	/**
	 * Metodo che configura il router per essere usato con un sistema multilingua
	 *
	 * @param array $languages Lista delle lingue accettate
	 * @param string $defaultLang Lingua di default
	 * @param string $currentLang Lingua corrente. Se non impostata è valida quella di default
	 * 
	 * @throws exception\RouterException 
	 */
	public function configureMultilanguage(array $languages, $defaultLang, $currentLang = null)
	{
		$this->isMultilanguages = true;
		$this->multilanguagesAccepted = $languages;
		
		if(!in_array($defaultLang, $languages))
			throw new exception\RouterException($defaultLang, exception\RouterException::LANG_NOT_FOUND);
		
		$this->defaultLang = $defaultLang;
		
		if($currentLang !== null && !in_array($currentLang, $languages))
			throw new exception\RouterException($currentLang, exception\RouterException::LANG_NOT_FOUND);
		
		$this->currentLang = $currentLang;
	}

	/**
	 * Metodo che aggiunge una route da inserire nel controllo della url
	 *
	 * @param string $route Route da verificare
	 * @param string $controller Nome del controller
	 * @param string $action Nome della action
	 * @param array $otherParam Parametri aggiuntivi da restituire nel caso che il controllo della route abbia esito positivo
	 *
	 * @return Route
	 */
	public function route($name, $route, $controller, $action, array $otherParam = array())
	{
		$route = new \plugin\router\src\Route(&$this, $name, $route, $controller, $action, $otherParam);
		$this->_routes[$name] = $route;

		return $route;
	}
	
	/**
	 * Lista delle Route
	 *
	 * @return array
	 */
	public function getRoutes()
	{
		return $this->_routes;
	}
}
?>